<?php
require_once('application/libraries/show.php');
require_once('application/libraries/person.php');
class Add extends MY_Controller{
    function __construct(){
    	parent::__construct();
    	
    }
	function person(){
		if($this->isLoggedIn() != TRUE){
    		$this->load->view('noAccess');
    		$this->load->view('includes/footer');
    	}
    	else{
    		$this->load->view('addPersonform');
    		$this->load->view('includes/footer');
    	}
	}
	function show(){
		if($this->isLoggedIn() != TRUE){
    		$this->load->view('noAccess');
    		$this->load->view('includes/footer');
    	}
    	else{
    		$this->load->view('addShowform');
    		$this->load->view('includes/footer');
    	}
	}
	function saveShow(){
		$this->load->library('form_validation');
		// Set validation rules, all fields required, correct email and matching passwords
		$this->form_validation->set_rules(NAME, 'Name', 'trim|required');
		$this->form_validation->set_rules(START_YEAR, 'Startyear', 'trim|required');
		$this->form_validation->set_rules(END_YEAR, 'Endyer', 'trim');
		$this->form_validation->set_rules(DESCRIPTION, 'Description', 'trim|required');
		if($this->form_validation->run() == FALSE){
			// If input is not valid, reload registration page
			$this->show();
		}
		else{
			$this->load->model('showHandler');
			$params = array(
				ID => NULL,
				NAME => $this->input->POST(NAME),
				START_YEAR => $this->input->POST(START_YEAR),
				END_YEAR => $this->input->POST(END_YEAR),
				DESCRIPTION => $this->input->POST(DESCRIPTION)
			);
			$show = new Show($params);
			if($query = $this->showHandler->addShow($show)){
				// If registration is a success redirect to success-page and add successmessages
								$this->load->model('showDAL');
				$showID = $this->showDAL->getShowID($show->getName());
				$data['successAction'] = $show->getName() . ' was successfully added to database.';
				$data['successRedirectLink'] = "/displaySingle/show/$showID";
				$data['successRedirectText'] = 'Go to show.';
				$data['main_content'] = 'success';
				$this->load->view('includes/template', $data);
			}
			else{
				// If registration was not a success, reload showform
				$this->show();
			}
		}
	}
	function savePerson(){
		$this->load->library('form_validation');
		// Set validation rules, all fields required, correct email and matching passwords
		$this->form_validation->set_rules(FIRSTNAME, 'First name', 'trim|required');
		$this->form_validation->set_rules(LASTNAME, 'Last name', 'trim|required');
		//$this->form_validation->set_rules(DESCRIPTION, 'Description', 'trim|required');
		if($this->form_validation->run() == FALSE){
			// If input is not valid, reload registration page
			$this->person();
		}
		else{
			$this->load->model('showHandler');
			$params = array(
				ID => NULL,
				FIRSTNAME => $this->input->POST(FIRSTNAME),
				LASTNAME => $this->input->POST(LASTNAME),
				DESCRIPTION => $this->input->POST(DESCRIPTION)
			);
			$person = new Person($params);
			if($query = $this->showHandler->addPerson($person)){
				$this->load->model('showDAL');
				//$firstname = $person->getFirst
				$personID = $this->showDAL->getPersonID($person->getFirstname(), $person->getLastname());
				// If registration is a success redirect to success-page and add successmessages
				$data['successAction'] = $person->getFirstname() . ' ' . $person->getLastname() . ' was successfully added to database.';
				$data['successRedirectLink'] = "/displaySingle/person/$personID";
				$data['successRedirectText'] = 'Go to person.';
				$data['main_content'] = 'success';
				$this->load->view('includes/template', $data);
			}
			else{
			echo "fel";
				// If registration was not a success, reload showform
				$this->person();
			}
		}
	}
}